/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.Pcheck;
import kimo.persistence.entities.User;
import kimo.persistence.entities.Producer;

/**
 *
 * @author a381u296
 */
public class PcheckJpaController implements Serializable {

    public PcheckJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Pcheck pcheck) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            User checkUserId = pcheck.getCheckUserId();
            if (checkUserId != null) {
                checkUserId = em.getReference(checkUserId.getClass(), checkUserId.getUserId());
                pcheck.setCheckUserId(checkUserId);
            }
            Producer checkProducerId = pcheck.getCheckProducerId();
            if (checkProducerId != null) {
                checkProducerId = em.getReference(checkProducerId.getClass(), checkProducerId.getProducerId());
                pcheck.setCheckProducerId(checkProducerId);
            }
            em.persist(pcheck);
            if (checkUserId != null) {
                checkUserId.getPcheckList().add(pcheck);
                checkUserId = em.merge(checkUserId);
            }
            if (checkProducerId != null) {
                checkProducerId.getPcheckList().add(pcheck);
                checkProducerId = em.merge(checkProducerId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Pcheck pcheck) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pcheck persistentPcheck = em.find(Pcheck.class, pcheck.getCheckId());
            User checkUserIdOld = persistentPcheck.getCheckUserId();
            User checkUserIdNew = pcheck.getCheckUserId();
            Producer checkProducerIdOld = persistentPcheck.getCheckProducerId();
            Producer checkProducerIdNew = pcheck.getCheckProducerId();
            if (checkUserIdNew != null) {
                checkUserIdNew = em.getReference(checkUserIdNew.getClass(), checkUserIdNew.getUserId());
                pcheck.setCheckUserId(checkUserIdNew);
            }
            if (checkProducerIdNew != null) {
                checkProducerIdNew = em.getReference(checkProducerIdNew.getClass(), checkProducerIdNew.getProducerId());
                pcheck.setCheckProducerId(checkProducerIdNew);
            }
            pcheck = em.merge(pcheck);
            if (checkUserIdOld != null && !checkUserIdOld.equals(checkUserIdNew)) {
                checkUserIdOld.getPcheckList().remove(pcheck);
                checkUserIdOld = em.merge(checkUserIdOld);
            }
            if (checkUserIdNew != null && !checkUserIdNew.equals(checkUserIdOld)) {
                checkUserIdNew.getPcheckList().add(pcheck);
                checkUserIdNew = em.merge(checkUserIdNew);
            }
            if (checkProducerIdOld != null && !checkProducerIdOld.equals(checkProducerIdNew)) {
                checkProducerIdOld.getPcheckList().remove(pcheck);
                checkProducerIdOld = em.merge(checkProducerIdOld);
            }
            if (checkProducerIdNew != null && !checkProducerIdNew.equals(checkProducerIdOld)) {
                checkProducerIdNew.getPcheckList().add(pcheck);
                checkProducerIdNew = em.merge(checkProducerIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pcheck.getCheckId();
                if (findPcheck(id) == null) {
                    throw new NonexistentEntityException("The pcheck with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pcheck pcheck;
            try {
                pcheck = em.getReference(Pcheck.class, id);
                pcheck.getCheckId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pcheck with id " + id + " no longer exists.", enfe);
            }
            User checkUserId = pcheck.getCheckUserId();
            if (checkUserId != null) {
                checkUserId.getPcheckList().remove(pcheck);
                checkUserId = em.merge(checkUserId);
            }
            Producer checkProducerId = pcheck.getCheckProducerId();
            if (checkProducerId != null) {
                checkProducerId.getPcheckList().remove(pcheck);
                checkProducerId = em.merge(checkProducerId);
            }
            em.remove(pcheck);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Pcheck> findPcheckEntities() {
        return findPcheckEntities(true, -1, -1);
    }

    public List<Pcheck> findPcheckEntities(int maxResults, int firstResult) {
        return findPcheckEntities(false, maxResults, firstResult);
    }

    private List<Pcheck> findPcheckEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Pcheck.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Pcheck findPcheck(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Pcheck.class, id);
        } finally {
            em.close();
        }
    }

    public int getPcheckCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Pcheck> rt = cq.from(Pcheck.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
